package com.example.expinterview.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.example.expinterview.entity.EiUser;
import com.example.expinterview.entity.EiUserIntegralValueConfig;
import com.example.expinterview.entity.EiUserLoginLog;
import com.example.expinterview.entity.ResultBody;
import com.example.expinterview.entity.enums.EiUserIntegralUseEnum;
import com.example.expinterview.entity.enums.ErrorCodeEnum;
import com.example.expinterview.mapper.EiUserIntegralValueConfigMapper;
import com.example.expinterview.mapper.EiUserLoginLogMapper;
import com.example.expinterview.mapper.EiUserMapper;
import com.example.expinterview.service.IEiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @classname: EiUserServiceImpl
 * @description: TODO
 * @author: kaiser
 * @date: 2021/10/18 22:02
 * @version: 1.0
 */
@Service
public class EiUserServiceImpl extends ServiceImpl<EiUserMapper, EiUser> implements IEiUserService {
    @Autowired
    private EiUserLoginLogMapper eiUserLoginLogMapper;
    @Autowired
    private EiUserIntegralValueConfigMapper eiUserIntegralValueConfigMapper;

    @Override
    public ResultBody selectByOpenid(String openid) {
        EiUser user = baseMapper.selectOne(new QueryWrapper<EiUser>().eq("openid", openid));
        if (Objects.isNull(user)) {
            return ResultBody.empty();
        } else {
            // 插入登陆日志
            EiUserLoginLog eiUserLoginLog = new EiUserLoginLog(user.getId());
            eiUserLoginLogMapper.insert(eiUserLoginLog);
            return ResultBody.success(user);
        }
    }

    @Override
    public ResultBody createUser(EiUser user) {
        boolean b = StrUtil.isBlank(user.getOpenid());
        if (b) {
            return ResultBody.fail(ErrorCodeEnum.PARAMS_ERROR);
        } else {
            boolean bo;
            Long tempId;
            EiUser u = baseMapper.selectOne(new QueryWrapper<EiUser>().eq("openid", user.getOpenid()));
            if (Objects.isNull(u)) {
                EiUserIntegralValueConfig eiUserIntegralValueConfig = eiUserIntegralValueConfigMapper.selectOne(
                        Wrappers.<EiUserIntegralValueConfig>lambdaQuery()
                                .eq(EiUserIntegralValueConfig::getIntegralUseId, EiUserIntegralUseEnum.CREATE_USER.getValue())
                );
                try {
                    user.setIntegral(eiUserIntegralValueConfig.getValue());
                } catch (Exception e) {
                    user.setIntegral(20);
                }

                bo = SqlHelper.retBool(baseMapper.insert(user));
                tempId = user.getId();
            } else {
                user.setId(u.getId());
                user.setIntegral(u.getIntegral());
                bo = SqlHelper.retBool(baseMapper.updateById(user));
                tempId = u.getId();
            }
            if (bo) {
                return ResultBody.success(baseMapper.selectById(tempId));
            } else {
                return ResultBody.fail(ErrorCodeEnum.DB_FAIL);
            }
        }
    }
}
